package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DBUtil;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.domain.vo.Page;

/**
 * 家庭档案信息管理dao类
 * @author 沉羽墨玖
 *
 */
public class FamilyDao extends BaseDao {
	private static final String SQL_SELECT_FAMCODE = "select * from family where famCode=?";

	@Override
	protected Family handle(ResultSet rs) throws Exception {
		Family family = new Family();
		family.setFamCode(rs.getString("famCode"));
		family.setTown(new Area(rs.getString("town")));
		family.setVillage(new Area(rs.getString("village")));
		family.setGroupNo(new Area(rs.getString("groupNo")));
		family.setHousePro(rs.getString("housePro"));
		family.setPopuNum(rs.getInt("popuNum"));
		family.setAgriNum(rs.getInt("agriNum"));
		family.setAddress(rs.getString("address"));
		family.setCreateTime(rs.getDate("createTime"));
		family.setCreator(rs.getString("creator"));

		Person holder = new Person(rs.getString("persCode"));
		family.setHolder(holder);
		return family;
	}

	/**
	 * 查找对应的家庭档案记录并转为对象实例
	 * 
	 * @param famCode
	 * @return
	 * @throws Exception
	 */
	public Family get(String famCode) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement(SQL_SELECT_FAMCODE);
			int index = 1;
			pstmt.setString(index, famCode);
			rs = pstmt.executeQuery();
			return handle(rs);
		} catch (SQLException e) {
			throw new SQLException("读取famCode记录失败", e);
		}
	}

	/**
	 * 分页
	 * 
	 * @param 如se
	 *            ;e * from family where famCode=?
	 * @param params
	 * @param page
	 * @param size
	 * @return
	 * @throws Exception
	 */
	public Page queryOjectsByPage(String sql, Object[] params, int pageNo,
			int size) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Page page = null;
		List<Family> datas = new ArrayList<Family>();
		try {
			int start = (pageNo - 1) * size;
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement(sql + " limit " + start + "," + size);
			int index = 1;
			if (params != null) {
				for (Object param : params) {
					if (param instanceof String) {
						pstmt.setString(index++, (String) param);
					}
					if (param instanceof Integer) {
						pstmt.setInt(index++, (Integer) param);
					}
					if (param instanceof Float) {
						pstmt.setFloat(index++, (Float) param);
					}
				}
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Family family = handle(rs);
				datas.add(family);
			}
			// 统计SQL对应记录数
			int total = count(sql, params);
			page = new Page(total, pageNo, size, datas);
			return page;
		} catch (SQLException e) {
			throw new SQLException("执行SQL[" + sql + "]失败", e);
		}

	}
}
